package com.server.user;

import java.util.Map;

import org.hibernate.validator.internal.util.privilegedactions.SetAccessibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.config.AppReturn;
import com.config.ConstService;
import com.config.PassInter;
import com.util.StrKit;

@RestController
@RequestMapping("/appUser/user")
public class UserCtrl extends AppUserBaseCtrl{
	@Autowired
	UserService userService;
	@Autowired
	ConstService constService;
	
	/**
	 * 跳转到注册页面
	 * @return
	 */
//	@RequestMapping("/goReg/${tjcode}")//无需关注公众号是，封装请求直接用restful
	@RequestMapping("/goReg")
	@PassInter
	public ModelAndView goReg(@RequestParam(name="tjcode",required=false) String tjcode)
	{
		ModelAndView mav = new ModelAndView("reg/reg");
		String openid = "";
		if(getSession().getAttribute("openid")!=null)openid = (String) getSession().getAttribute("openid");
		mav.addObject("openid", openid);
		if(StrKit.isBlank(tjcode))tjcode = constService.getTjcodeByOpenid(openid);//如果入参没有，从redis中寻找
		mav.addObject("tjcode", tjcode);
		return mav;
	}
	/**
	 * 跳转到登录页面
	 * @return
	 */
//	@RequestMapping("/goReg/${tjcode}")//无需关注公众号是，封装请求直接用restful
	@RequestMapping("/goLog")
	public ModelAndView goLog()
	{
		ModelAndView mav = new ModelAndView("reg/log");
		return mav;
	}
	/**
	 * 发短信
	 * @param phone
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/sendMSG")
	@PassInter
	public String sendMSG(@RequestParam(name="phone")String phone)
	{
		AppReturn ret = userService.sendSms(phone);
		return ret.toString();
	}
	
	//检测手机号是否被注册
	@ResponseBody
	@RequestMapping("/checkPhone")
	@PassInter
	public String checkPhone(@RequestParam("phone")String phone)
	{
		AppReturn ret = userService.checkPhone(phone);
		return ret.toString();
	}

	/**
	 * 用户注册
	 * @param account
	 * @param pwd
	 * @param phone
	 * @param code
	 * @param tjcode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userReg")
	public String userReg(@RequestParam(name="pwd",required=false)String pwd,@RequestParam(name="phone")String phone,@RequestParam(name="code",required=false)String code,@RequestParam(name="realname",required=false)String realname
			,@RequestParam(name="tjcode")String tjcode,@RequestParam(name="openid")String openid)
	{
		AppReturn ret = userService.doReg(tjcode.toUpperCase(), phone, code,pwd,realname,openid);
		return ret.toString();
	}
	
	
	/**
	 * 登录
	 * @param type 登录方式1：手机号+密码。2：手机号+验证码
	 * @param account 用户名
	 * @param pwd     密码
	 * @param phone   手机号
	 * @param code    验证码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login")
	public String login(@RequestParam(name="type",required=false,defaultValue="3")String type
			,@RequestParam(name="pwd",required=false)String pwd
			,@RequestParam(name="phone")String phone,@RequestParam(name="code",required=false)String code,@RequestParam(name="no",required=false)String no,@RequestParam(name="UUID",required=false)String UUID)
	{
		String openid = (String) getSession().getAttribute("openid");
		AppReturn ret = userService.doLog(phone, pwd, type, code, UUID,openid,no.toUpperCase());
		return ret.toString();
	}

	/**
	 * 自动登录——考虑是否使用
	 * @param type
	 * @param account
	 * @param pwd
	 * @param phone
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/autoLogin")
	public String autoLogin(@RequestParam(name="account",required=false)String account,@RequestParam(name="pwd",required=false)String pwd)
	{
		
		return null;
	}
	
	/**
	 * 获取用户信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getUserInfo")
	public String getUserInfo()
	{
		refreshUser();
		Map<String, Object> user = getLogUser();
		AppReturn ret = StrKit.map2AppReturn(user);
		return ret.toString();
	}
	
}
